package com.am.lottery.db

import com.zaxxer.hikari.HikariDataSource
import org.flywaydb.core.Flyway
import org.ktorm.database.Database
import org.slf4j.Logger
import org.slf4j.LoggerFactory

object Db {
    private val logger: Logger = LoggerFactory.getLogger(this::class.java)
    private const val URL = "jdbc:p6spy:sqlite:lottery.db"
    private const val DRIVER = "com.p6spy.engine.spy.P6SpyDriver"

    private val dataSource = HikariDataSource().apply {
        this.jdbcUrl = URL
        this.driverClassName = DRIVER
    }

    val database: Database by lazy {
        val d = Database.connect(dataSource)
        logger.info("Connected to database")
        d
    }

    fun init() {
        flyway()
    }

    private fun flyway() {
        val flyway = Flyway.configure().dataSource(dataSource)
            .locations("")
            .load()
        flyway.migrate()
    }


}